
<!DOCTYPE html>
<html lang="zh-cn">
    <head>
        <meta charset="utf-8">
        <title>浏览页面</title>
        <link rel="stylesheet" href="/app/admin/component/pear/css/pear.css" />
        <link rel="stylesheet" href="/app/admin/admin/css/reset.css" />
        <style>
            audio {
                width: 300px;
                height: 40px;
            }
        </style>
    </head>
    <body class="pear-container">
    
        <!-- 顶部查询表单 -->
        <div class="layui-card">
            <div class="layui-card-body">
                <form class="layui-form top-search-from">
                    <div class="layui-form-item">
                        <label class="layui-form-label">名称</label>
                        <div class="layui-input-block">
                            <div class="layui-input-block">
                                <input type="hidden" autocomplete="off" name="name[]" value="like" class="layui-input inline-block">
                                <input type="text" autocomplete="off" name="name[]" class="layui-input">
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">素材</label>
                        <div class="layui-input-block">
                            <div name="material_data_id" id="material_data_id" value="" ></div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">分类</label>
                        <div class="layui-input-block">
                            <div name="catid" id="catid" value="" ></div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">状态</label>
                        <div class="layui-input-block">
                            <div name="status" id="status" value="" ></div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">添加时间</label>
                        <div class="layui-input-block">
                            <div class="layui-input-block" id="created_at">
                                <input type="text" autocomplete="off" name="created_at[]" id="created_at-date-start" class="layui-input inline-block" placeholder="开始时间">
                                -
                                <input type="text" autocomplete="off" name="created_at[]" id="created_at-date-end" class="layui-input inline-block" placeholder="结束时间">
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item layui-inline">
                        <label class="layui-form-label"></label>
                        <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="table-query">
                            <i class="layui-icon layui-icon-search"></i>查询
                        </button>
                        <button type="reset" class="pear-btn pear-btn-md" lay-submit lay-filter="table-reset">
                            <i class="layui-icon layui-icon-refresh"></i>重置
                        </button>
                    </div>
                    <div class="toggle-btn">
                        <a class="layui-hide">展开<i class="layui-icon layui-icon-down"></i></a>
                        <a class="layui-hide">收起<i class="layui-icon layui-icon-up"></i></a>
                    </div>
                </form>
            </div>
        </div>
        
        <!-- 数据表格 -->
        <div class="layui-card">
            <div class="layui-card-body">
                <table id="data-table" lay-filter="data-table"></table>
            </div>
        </div>

        <!-- 表格顶部工具栏 -->
        <script type="text/html" id="table-toolbar">
            <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="sysncallmaterial" permission="app.admin.material.index.sysncallmaterial">
                <i class="layui-icon layui-icon-add-1"></i>同步素材视频
            </button>
            <!-- <button class="pear-btn pear-btn-danger pear-btn-md" lay-event="batchRemove" permission="app.admin.material.index.deletecos">
                <i class="layui-icon layui-icon-delete"></i>删除
            </button> -->
        </script>

        <!-- 表格行工具栏 -->
        <script type="text/html" id="table-bar">
            <button class="pear-btn pear-btn-xs tool-btn" lay-event="remove" permission="app.admin.material.index.delete">删除</button>
        </script>

        <script src="/app/admin/component/layui/layui.js?v=2.8.12"></script>
        <script src="/app/admin/component/pear/pear.js"></script>
        <script src="/app/admin/admin/js/permission.js"></script>
        <script src="/app/admin/admin/js/common.js"></script>
        <script src="/app/admin/admin/js/functions.js"></script>
        <script src="/app/admin/admin/js/md5.js"></script>
        
        <script>

            // 相关常量
            const PRIMARY_KEY = "id";
            const PRIMARY_KEY_VALUES = layui.url().search['ids'];
            const SELECT_API = "/app/admin/material/index/selectcos" + location.search;
            const SYSN_URL = "/app/admin/material/index/sysncallmaterial";
            const UPDATE_API = "/app/admin/material/index/update";
            const DELETE_API = "/app/admin/material/index/deletecos";
            const INSERT_URL = "/app/admin/material/index/insert";
            const UPDATE_URL = "/app/admin/material/index/update";
            const CATEGORY_URL = "/app/admin/material/category/index";
            const VIDEO_VIEW_URL = "/app/admin/material/index/video";

            let where = {};
            if(PRIMARY_KEY_VALUES){
                where = {'ids':PRIMARY_KEY_VALUES}
            }

            // 字段 添加时间 created_at
            layui.use(["laydate"], function() {
                layui.laydate.render({
                    elem: "#created_at",
                    range: ["#created_at-date-start", "#created_at-date-end"],
                    type: "datetime",
                    mark: date_today_mark,
                });
            })

            // 字段  catid
            layui.use(["jquery", "xmSelect", "popup"], function() {
                layui.$.ajax({
                    url: "/app/admin/material/category/select?format=select&limit=1000",
                    dataType: "json",
                    async:true,
                    headers: get_header({"format":"select"}),
                    success: function (res) {
                        layui.xmSelect.render({
                            el: "#catid",
                            name: "catid",
                            filterable: true,
                            data: res.data,
                            model: {"icon":"hidden","label":{"type":"text"}},
                            clickClose: true,
                            radio: true,
                        })
                        if (res.code) {
                            layui.popup.failure(res.msg);
                        }
                    }
                });
            });

            // 字段  material_data_id
            layui.use(["jquery", "xmSelect", "popup"], function() {
                layui.$.ajax({
                    url: "/app/admin/material/index/select?format=select&limit=1000",
                    dataType: "json",
                    async:true,
                    headers: get_header({"format":"select"}),
                    success: function (res) {
                        layui.xmSelect.render({
                            el: "#material_data_id",
                            name: "material_data_id",
                            filterable: true,
                            data: res.data,
                            model: {"icon":"hidden","label":{"type":"text"}},
                            clickClose: true,
                            radio: true,
                        })
                        if (res.code) {
                            layui.popup.failure(res.msg);
                        }
                    }
                });
            });

            // 字段 状态 status
            layui.use(["jquery", "xmSelect"], function() {
                let value = layui.$("#status").attr("value");
                let initValue = value ? value.split(",") : [];
                layui.xmSelect.render({
                    el: "#status",
                    name: "status",
                    filterable: true,
                    initValue: initValue,
                    data: [{"value":"1","name":"是"},{"value":"0","name":"否"}],
                    model: {"icon":"hidden","label":{"type":"text"}},
                    clickClose: true,
                    radio: true
                })
            });

            // 表格渲染
            layui.use(["table", "form", "common", "popup", "util"], function() {
                let table = layui.table;
                let form = layui.form;
                $ = layui.$;
                let common = layui.common;
                let util = layui.util;
                
				// 表头参数
				let cols = [
					{
						type: "checkbox", align: "center"
					},{
						title: "ID",align: "center", field: "id",width:100,sort:true,
					},{
						title: "素材",align: "center", field: "material_data_name",width:120,sort:true,
					},{
						title: "分类",align: "center", field: "catname",width:120,sort:true,
					},{
						title: "名称",align: "center", field: "name",width:200,sort:true, templet: function (d) {
                            return decodeURI(d.name);
                        }
					},
                    {
                        title: '视频',align: "center", field: 'video', templet:function (d) {
                            if(d.video){
                                return '<div data-video="'+d.video+'" onclick="open_video(this)">' +
                                    '<video width="60" height="60">'+
                                    '<source src="'+d.video+'" type="video/mp4">'+
                                    '<object data="'+d.video+'" width="500" height="800">'+
                                    '</object> '+
                                    '</video>' +
                                    '</div>';
                            }else{
                                return ' ';
                            }
                        }
                    }/*,{
						title: "状态",align: "center", field: "status",width:100, templet: function (d) {
							let field = "status";
							form.on("switch("+field+")", function (data) {
								let load = layer.load();
								let postData = {};
								postData[field] = data.elem.checked ? 1 : 0;
								postData[PRIMARY_KEY] = this.value;
                                $.ajax({
                                    url: UPDATE_API,
                                    dateType: "json",
                                    type: "POST",
                                    data:postData,
                                    headers: get_header(postData),
                                    success: function (res) {
                                        layer.close(load);
                                        if (res.code) {
                                            return layui.popup.failure(res.msg, function () {
                                                data.elem.checked = !data.elem.checked;
                                                form.render();
                                            });
                                        }
                                        return layui.popup.success("操作成功");
                                    }
                                });
							});
							let checked = d[field] === 1 ? "checked" : "";
							return '<input type="checkbox" value="'+util.escape(d[PRIMARY_KEY])+'" lay-filter="'+util.escape(field)+'" lay-skin="switch" lay-text="'+util.escape('')+'" '+checked+'/>';
						}
					}*/,{
                        title: "添加时间",align: "center", field: "created_at",width: 180,
                    },{
						title: "操作", toolbar: "#table-bar", align: "center", fixed: "right", width: 180,
					}
				];
				
				// 渲染表格
				table.render({
				    elem: "#data-table",
				    url: SELECT_API,
				    page: true,
                    headers: get_header(where),
				    cols: [cols],
				    skin: "line",
				    size: "lg",
				    toolbar: "#table-toolbar",
				    autoSort: false,
				    defaultToolbar: [{
				        title: "刷新",
				        layEvent: "refresh",
				        icon: "layui-icon-refresh",
				    }, "filter", "print", "exports"],
				    done: function () {
				        layer.photos({photos: 'div[lay-id="data-table"]', anim: 5});
				    }
				});
				
				// 获取表格中下拉或树形组件数据
				let apiResults = {};
				apiResults["position"] = [];
                // 编辑或删除行事件
                table.on("tool(data-table)", function(obj) {
                    if (obj.event === "remove") {
                        remove(obj);
                    } else if (obj.event === "edit") {
                        edit(obj);
                    }
                });

                // 表格顶部工具栏事件
                table.on("toolbar(data-table)", function(obj) {
                    if (obj.event === "sysncallmaterial") {
                        sysncallmaterial();
                    } else if (obj.event === "refresh") {
                        refreshTable();
                    } else if (obj.event === "batchRemove") {
                        batchRemove(obj);
                    } else if (obj.event === "catgory") {
                        catgory(obj);
                    }
                });

                // 表格顶部搜索事件
                form.on("submit(table-query)", function(data) {
                    where = searchWhere(data.field);
                    table.reload("data-table", {
                        page: {
                            curr: 1
                        },
                        where: where,
                        headers: get_header(where)
                    })
                    return false;
                });
                
                // 表格顶部搜索重置事件
                form.on("submit(table-reset)", function(data) {
                    where = {};
                    table.reload("data-table", {
                        where: where,
                        headers: get_header(where)
                    })
                });

                // 表格排序事件
                table.on("sort(data-table)", function(obj){
                    where.field=obj.field;
                    where.order=obj.type;
                    table.reload("data-table", {
                        initSort: obj,
                        scrollPos: "fixed",
                        where: where,
                        headers: get_header(where)
                    });
                });

                // 同步全部素材视频
                let sysncallmaterial = function() {
                    
                    layer.confirm("确定同步全部素材视频?", {
                        icon: 3,
                        title: "提示"
                    }, function(index) {
                        layer.close(index);
                        let loading = layer.load();

                        layui.$.ajax({
                            url: "/app/admin/material/index/select?limit=1000",
                            dataType: "json",
                            async:true,
                            headers: get_header(),
                            success: function (res) {
                                if (res.code) {
                                    layui.popup.failure(res.msg);
                                }
                                var num = 0;
                                var timer = setInterval(function() {
                                    layui.popup.success("正在同步第" + (num+1) + "/" + res.data.length + "个素材：" + res.data[num].name);
                                    sysncallmaterialbycateogrory(res.data[num]);
                                    num++;
                                    if(num == res.data.length){
                                        //clearInterval()可以用来关闭一个定时器，参数为定时器标识，关闭指定定时器
                                        clearInterval(timer);
                                        return layui.popup.success("操作成功", refreshTable);
                                    }
                                }, 1000);
                                layer.close(loading);
                            }
                        });

                        
                    });

                }

                // 按分类同步素材视频
                let sysncallmaterialbycateogrory = function(obj) {
                    let data = {};
                    data['s_id'] = obj.id;
                    data['catid'] = obj.catid;
                    data['heigh_material_path'] = obj.heigh_material_path;
                    data['normal_material_path'] = obj.normal_material_path;
                    $.ajax({
                        url: SYSN_URL,
                        data: data,
                        dataType: "json",
                        type: "post",
                        async:true,
                        headers: get_header(data),
                        success: function(res) {
                            if (res.code) {
                                return layui.popup.failure(res.msg);
                            }
                        }
                    })
                }

                // 分类管理
                let catgory = function() {
                    layer.open({
                        type: 2,
                        title: "分类管理",
                        shade: 0.1,
                        maxmin: true,
                        area: [common.isModile()?"100%":"950px", common.isModile()?"100%":"550px"],
                        content: CATEGORY_URL
                    });
                }

                // 表格编辑数据
                let edit = function(obj) {
                    let value = obj.data[PRIMARY_KEY];
                    layer.open({
                        type: 2,
                        title: "修改",
                        shade: 0.1,
                        maxmin: true,
                        area: [common.isModile()?"100%":"95%", common.isModile()?"100%":"95%"],
                        content: UPDATE_URL + "?" + PRIMARY_KEY + "=" + value
                    });
                }

                // 删除一行
                let remove = function(obj) {
                    return doRemove(obj.data[PRIMARY_KEY]);
                }

                // 删除多行
                let batchRemove = function(obj) {
                    let checkIds = common.checkField(obj, PRIMARY_KEY);
                    if (checkIds === "") {
                        layui.popup.warning("未选中数据");
                        return false;
                    }
                    doRemove(checkIds.split(","));
                }

                // 执行删除
                let doRemove = function (ids) {
                    let data = {};
                    data[PRIMARY_KEY] = ids;
                    layer.confirm("确定删除?删除将同步删除存储桶内的文件", {
                        icon: 3,
                        title: "提示"
                    }, function(index) {
                        layer.close(index);
                        let loading = layer.load();
                        $.ajax({
                            url: DELETE_API,
                            data: data,
                            dataType: "json",
                            type: "post",
                            async:true,
                            headers: get_header(data),
                            success: function(res) {
                                layer.close(loading);
                                if (res.code) {
                                    return layui.popup.failure(res.msg);
                                }
                                return layui.popup.success("操作成功", refreshTable);
                            }
                        })
                    });
                }

                // 刷新表格数据
                window.refreshTable = function(param) {
                    table.reloadData("data-table", {
                        scrollPos: "fixed"
                    });
                }
                window.view_content = function (id,type) {
                    layer.open({
                        type: 2,
                        title: "视频明细",
                        shade: 0.1,
                        maxmin: true,
                        area: [common.isModile()?"100%":"95%", common.isModile()?"100%":"95%"],
                        content: VIDEO_VIEW_URL + "?" + PRIMARY_KEY + "=" + id+'&type='+type
                    });
                }
            })

        </script>
    </body>
</html>
